package rmhc
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.text.TextField;
	
	import rmhc.design.StatusDesign;
	import rmhc.gui.PostView;
	import rmhc.gui.PotatoeDisplay;
	
	[SWF(width='607', height='412', backgroundColor='#FFFFFF', frameRate='30')]
	public class RMHCWall extends MovieClip
	{
		protected static var wall:RMHCWall;
		
		public var disp:PotatoeDisplay;
		
		protected var postId:String;
		protected var view:PostView;
		
		public function RMHCWall()
		{
			super();
			wall = this;
			addEventListener( Event.ADDED_TO_STAGE , init );
		}
		protected function init( e:Event ):void
		{
			scaleX = scaleY = stage.stageWidth / 607;
			//stage.scaleMode = StageScaleMode.EXACT_FIT;
			//x = stage.stageWidth - 607;
			//y = stage.stageHeight - 412;
			
			//парсим ИД поста
			view = new PostView();
			addChild( view );
			//postId = stage.loaderInfo.parameters.post_id;
			postId = ( parent as IAppPreloader ).getPostId();
			trace( "postId" , postId );
			//TODO: получение данных с сервера, отображение поста
			var v:URLVariables = new URLVariables();
			v.postId = postId;
			
			var req:URLRequest = new URLRequest( getServerUrl() + "potatoeGet.php" );
			req.data = v;
			
			var uv:URLLoader = new URLLoader();
			uv.dataFormat = URLLoaderDataFormat.TEXT;
			uv.addEventListener(Event.COMPLETE , onDataLoaded );
			uv.load( req );
			
			//td();
		}
		
		protected function td():void
		{
			var tf:TextField = new TextField();
			tf.width = 400;
			tf.height = 200;
			tf.border = tf.background = true;
			tf.x = tf.y = 200;
			tf.text = "stage " + stage.stageWidth + " " + stage.stageHeight + "\n";
			
			var pp:DisplayObjectContainer = view;
			while ( pp != null )
			{
				tf.appendText( "pp " + pp + "[" + pp.name + "] " + pp.width + "[" + pp.scaleX+ "], " + pp.height + "[" + pp.scaleY + "] , " + pp.x + "_" + pp.y + "\n" );
				pp = pp.parent;
			}
				
			addChild( tf );
		}
		
		protected function onDataLoaded( e:Event ):void
		{
			var uv:URLLoader = e.target as URLLoader;
			if ( uv.data == "0" )
			{
				return;
			}
			
			var resp:Object = JSON.decode( uv.data );
			
			view.setName( resp.senderFirst + " " + resp.senderLast );

			disp = new PotatoeDisplay( view.IMG_AREA.width , view.IMG_AREA.height );
			disp.x = view.IMG_AREA.x;
			disp.y = view.IMG_AREA.y;
			view.setContent( disp );
			disp.addEventListener(Event.CONNECT , onImgLoaded );
			disp.setPotatoe( getPotatoeSrc( resp.postType ) );
		}
		protected function onImgLoaded( e:Event ):void
		{
			view.showContent();
		}
		protected function getPotatoeSrc( num:int ):String
		{
			return getServerUrl() + "potat/" + num + ".png";
		}
		public function getServerUrl():String
		{
			try
			{
				var u:String = ( parent as IAppPreloader ).getServerUrl();
				return u;
			}
			catch( er:Error )
			{
			}
			return "http://rmhc/flash/";
			//return "http://mind2create.ru/vk/rmhc/";
		}
		
		public static function getWall():RMHCWall
		{
			return wall;
		}
		
		public function navigateToApp():void
		{
			( parent as IAppPreloader ).navigateToApp();
		}
	}
}